import 'package:flutter/material.dart';
import 'cached_image_widget.dart';

/// 图片工具类
/// 提供图片加载、缓存、占位符等功能
class ImageUtils {
  /// 获取占位符图片Widget
  static Widget getPlaceholderImage({
    double? width,
    double? height,
    Color? color,
    BoxFit fit = BoxFit.cover,
  }) {
    return Container(
      width: width,
      height: height,
      decoration: BoxDecoration(
        color: color ?? Colors.grey[200],
        borderRadius: BorderRadius.circular(8),
      ),
      child: Icon(
        Icons.image,
        size: (width != null && height != null) 
            ? (width + height) / 6 
            : 48,
        color: Colors.grey[400],
      ),
    );
  }

  /// 获取头像占位符Widget
  static Widget getAvatarPlaceholder({
    double size = 40,
    Color? backgroundColor,
    Color? iconColor,
  }) {
    return Container(
      width: size,
      height: size,
      decoration: BoxDecoration(
        color: backgroundColor ?? Colors.grey[300],
        shape: BoxShape.circle,
      ),
      child: Icon(
        Icons.person,
        size: size * 0.6,
        color: iconColor ?? Colors.grey[600],
      ),
    );
  }

  /// 安全加载网络图片，失败时显示占位符
  /// 🚀 已升级为带缓存的版本，图片会自动缓存到本地
  static Widget safeNetworkImage(
    String? imageUrl, {
    double? width,
    double? height,
    BoxFit fit = BoxFit.cover,
    BorderRadius? borderRadius,
  }) {
    return CachedImageWidget(
      imageUrl: imageUrl,
      width: width,
      height: height,
      fit: fit,
      borderRadius: borderRadius ?? BorderRadius.circular(8),
    );
  }

  /// 安全加载头像图片
  /// 🚀 已升级为带缓存的版本，头像会自动缓存到本地
  static Widget safeAvatarImage(
    String? avatarUrl, {
    double size = 40,
  }) {
    return CachedAvatarWidget(
      avatarUrl: avatarUrl,
      size: size,
    );
  }

  /// 加载封面图片（视频/文章封面）
  /// 支持显示播放按钮
  static Widget safeCoverImage(
    String? coverUrl, {
    double? width,
    double? height,
    BorderRadius? borderRadius,
    bool showPlayButton = false,
    VoidCallback? onTap,
  }) {
    return CachedCoverWidget(
      coverUrl: coverUrl,
      width: width,
      height: height,
      borderRadius: borderRadius,
      showPlayButton: showPlayButton,
      onTap: onTap,
    );
  }

  /// 清除图片缓存
  static Future<void> clearImageCache() async {
    await AppCacheManager.clearCache();
  }
}
